package me.notfire.limitedcraft;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Difficulty;
import org.bukkit.Statistic;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerStatisticIncrementEvent;
import org.bukkit.event.player.PlayerToggleSneakEvent;
import org.bukkit.event.player.PlayerToggleSprintEvent;
import org.bukkit.event.vehicle.VehicleEnterEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Objects;
import java.util.logging.Logger;

public final class LimitedCraft extends JavaPlugin implements Listener {

    @Override
    public void onEnable() {
        // Plugin startup logic
        getServer().getPluginManager().registerEvents(this, this);
        if(Objects.requireNonNull(getServer().getWorld("world")).getDifficulty() != Difficulty.HARD) {
            Logger logger = Bukkit.getLogger();
            logger.info("Difficulty corrected to HARD.");
            getServer().getWorld("world").setDifficulty(Difficulty.HARD);
        }
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic

    }

    // Detect if someone is sneaking and kill them.
    @EventHandler
    public void onPlayerToggleSneakEvent(PlayerToggleSneakEvent event) {
        Player player = event.getPlayer();
        player.sendMessage(ChatColor.RED + "You sneaked!");
        player.setHealth(0);
    }

    // Detect if someone is sneaking and kill them.
    @EventHandler
    public void onPlayerToggleSprintEvent(PlayerToggleSprintEvent event) {
        Player player = event.getPlayer();
        player.sendMessage(ChatColor.RED + "You sprinted!");
        player.setHealth(0);
    }

    // Detect if someone is in a vehicle and kill them.
    @EventHandler
    public void onVehicleEnterEvent(VehicleEnterEvent event) {
        Entity entity = event.getEntered();
        if(entity instanceof Player) {
            entity.sendMessage(ChatColor.RED +"You entered a " + event.getVehicle() + "!");
            ((Player) entity).setHealth(0);
        }
    }

    // Detect if someone jumps and kills them.
    @EventHandler
    public void onPlayerStatisticIncrementEvent(PlayerStatisticIncrementEvent event) {
        Player player = event.getPlayer();
        if(player.getStatistic(Statistic.JUMP) > 0) {
            player.setStatistic(Statistic.JUMP, 0);
            player.sendMessage(ChatColor.RED + "You jumped!");
            player.setHealth(0);
        }
    }

    // Detect if someone left clicks and cancel it.
    @EventHandler
    public void onPlayerInteractEvent(PlayerInteractEvent event) {
        Player player = event.getPlayer();
        if(event.getAction() == Action.LEFT_CLICK_AIR) {
            player.sendMessage(ChatColor.RED + "You left-clicked!");
            event.setCancelled(true);
        }
        else if(event.getAction() == Action.LEFT_CLICK_BLOCK) {
            player.sendMessage(ChatColor.RED + "You left-clicked!");
            event.setCancelled(true);
        }
    }

    // Detect if a player hits something and cancel it.
    @EventHandler
    public void onEntityDamageByEntityEvent(EntityDamageByEntityEvent event) {
        Entity entity = event.getDamager();
        if(entity instanceof Player) {
            entity.sendMessage(ChatColor.RED + "You hit something!");
            event.setCancelled(true);
        }
    }

    // Remove death messages.
    @EventHandler
    public void onPlayerDeathEvent(PlayerDeathEvent event) {
        event.setDeathMessage("");
    }
}
